package com.chujian.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.Utils.Util;
import com.chujian.model.Order;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class PayBillConsumer extends Activity{
	private RadioGroup mRadioGroup;
	private RadioButton mRbCash;
	private RadioButton mRbWechat;
	private RadioButton mRbAlipay;
	private Button mBtnPayConfirm;
	private TextView mTvTotalPrice;
	private TextView mTvOrderId;
	private Order mOrder;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str!=null){
				showDialog("已经成功通知到店家，请您准备好现金进行结账，谢谢！");
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_bill_consumer);
		initView();
		Intent intent=getIntent();
		if(intent!=null){
			String jsondata=intent.getStringExtra("orderInfo");
			mOrder=JsonUtil.OrderFromJson(jsondata);
			mTvTotalPrice.setText(mOrder.getTotalPrice()+"元");
			mTvOrderId.setText(mOrder.getOrderId()+"");
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		mRadioGroup=(RadioGroup) findViewById(R.id.rg_pay_bill_consumer);
		mRbCash=(RadioButton) findViewById(R.id.rb_pay_bill_c_cash);
		mRbWechat=(RadioButton) findViewById(R.id.rb_pay_bill_c_wechat);
		mRbAlipay=(RadioButton) findViewById(R.id.rb_pay_bill_c_alipay);
//		mBtnPayConfirm=(Button) findViewById(R.id.btn_pay_bill_c_confirm);
		mTvTotalPrice=(TextView) findViewById(R.id.tv_pay_bill_c_total_price);
		mTvOrderId=(TextView) findViewById(R.id.tv_pay_bill_c_order_id);
	}
	public void IbBack(View v){
		finish();
	}
	public void doClick(View v){
		if(mRbCash.isChecked()){
			Util.showTextLong(getApplicationContext(), "正在发起结账请求...");
			String url=Constan.BASE_URL+"PayBillConsumerServlet";
			Gson gson=new Gson();
			String jsonString=gson.toJson(mOrder);
			new HttpClientPostThread(url, mHandler, jsonString).start();
		}else if(mRbWechat.isChecked()){
//			ShowText.showText(getApplicationContext(), "wechat");
			showDialog("微信支付功能暂不可用，请选择现金支付，谢谢！");
		}else{
//			ShowText.showText(getApplicationContext(), "alipay");
			showDialog("支付宝支付功能暂不可用，请选择现金支付，谢谢！");
		}
	}
	
	
	private void showDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(PayBillConsumer.this);
//      builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("提示:");
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            }
        });
        builder.show();
	}
	

}
